import 'package:flutter/material.dart';

const Duration _kExpand = Duration(milliseconds: 200);
//折叠视图
class MyExpansionLayout extends StatefulWidget {
  const MyExpansionLayout({
    super.key,
    this.widgetId = 0,
    this.backgroundColor,
    this.onExpansionChanged,
    this.children = const <Widget>[],
    this.trailing,
    this.isExpanded,
    this.onClick,
    this.isCanExpanded = true,
  });

  final ValueChanged<bool>? onExpansionChanged;
  final List<Widget> children;
  final OnStatusChangeListener? onClick;
  final int widgetId;
  final Color? backgroundColor;
  final bool? isCanExpanded;

  //增加字段控制是否折叠
  final bool? isExpanded;

  final Widget? trailing;

  @override
  State<MyExpansionLayout> createState() => _MyExpansionLayoutState();
}

class _MyExpansionLayoutState extends State<MyExpansionLayout> with SingleTickerProviderStateMixin {
//折叠展开的动画，主要是控制height
  static final Animatable<double> _easeInTween = CurveTween(curve: Curves.easeIn);
  late AnimationController _controller;
  late Animation<double> _heightFactor;

  late bool _isExpanded;

  @override
  void initState() {
    super.initState();
    //初始化控制器以及出事状态
    _controller = AnimationController(duration: _kExpand, vsync: this);
    _heightFactor = _controller.drive(_easeInTween);
    _isExpanded = widget.isExpanded!;
    if (_isExpanded) _controller.value = 1.0;
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  void _handleTap() {
    setState(() {
      if (widget.isCanExpanded!) {
        _isExpanded = !_isExpanded;
        if (_isExpanded) {
          _controller.forward();
        } else {
          _controller.reverse();
        }
        //保存页面数据
        PageStorage.of(context).writeState(context, _isExpanded);
      }
    });
    //回调展开事件
    if (widget.onExpansionChanged != null) widget.onExpansionChanged!(_isExpanded);
  }

  @override
  Widget build(BuildContext context) {
    //执行以下对应的Tap事件
    final bool closed = !_isExpanded && _controller.isDismissed;
    return Column(
      children: [
        GestureDetector(
          behavior: HitTestBehavior.opaque,
          onTap: () {
            _handleTap();
            if (widget.onClick != null) {
              widget.onClick?.onStatusChange(widget.widgetId);
            }
          },
          child: widget.trailing,
        ),
        AnimatedBuilder(
          animation: _controller.view,
          builder: (context,child) {
return Column(
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        ClipRect(
          child: Align(
            heightFactor: _heightFactor.value,
            child: child,
          ),
        ),
      ],
    );
          },
          child: closed ? null : Column(children: widget.children),
        ),
        Visibility(
            visible: false,
            child: Container(
              alignment: Alignment.center,
              child: IconButton(
                  icon: _isExpanded ? const Icon(Icons.email) : const Icon(Icons.ac_unit),
                  onPressed: () {
                    _handleTap();
                  }),
            ))
      ],
    );
  }
}

abstract class OnStatusChangeListener {
  void onStatusChange(int widgetId);
}
